home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9870 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  87 lines

  1. Path: rain.fr!world-net!usenet
  2. From: Frederic LACHASSE <lachass@worldnet.fr>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] Simple question about constructors
  5. Date: Mon, 04 Mar 1996 22:00:20 +0000
  6. Organization: World-Net information exchange, Internet provider.
  7. Message-ID: <VA.00000055.007de71c@fred>
  8. References: <4hbadp$t0p@cloner4.netcom.com>
  9. Reply-To: lachass@worldnet.fr
  10. NNTP-Posting-Host: client77.sct.fr
  11. X-Newsreader: Virtual Access by Ashmount Research Ltd, http://www.ashmount.com
  12.  
  13. In article <4hbadp$t0p@cloner4.netcom.com>, stefmit@ix.netcom.com wrote:
  14. > It seems that the "copy constructor" day is one of my worst in the C++ 
  15. > learning process. Now I ran into another problem:
  16. > I have a linked list based on a template <class My_type>, which has a node 
  17. > class with (obviously) a pointer (let's call it *next) and an element field of 
  18. > the type My_type (called data). Then, I have a list class using the node 
  19. > above, with its own pointer *head (I am trying to build a sort of stack). I 
  20. > was able to build the stack (this was pretty easy), but then I tried to define 
  21. > a copy constructor in the list, using something similar to:
  22. > template <class My_type>
  23. > List<My_type> :: List(const List &newList)
  24. > { head -> data = newList.head -> data;
  25. >   head -> next = newList.head -> next; }
  26. > I am almost positive that the second line crashes my program when I try to 
  27. > intialize an object list based on an existing one (as I copy pointers, instead 
  28. > of what they point to - i.e. next), but I have no clue on how to address the 
  29. > problem. The example I have in my book deals with strings, thus the 
  30. > availability of strcpy or strup functions, that handle the copy. Would anybody 
  31. > care to enlighten me on this?
  32. > TIA.
  33.  
  34. I think you want to copy every element of the old list into the new one:
  35.  
  36. template <class My_type>
  37. class List
  38. {
  39.   class node   // definition of node
  40.   {
  41.   public:
  42.     My_type data;
  43.     node *next;
  44.     node(const My_type &d)
  45.     : data(d),  // data is initialized using its copy constructor
  46.       next(0)   // I like to initialized pointer to useful data
  47.     {} // nothing else to do
  48.   };
  49.   
  50.   node *head;
  51.  
  52. public:
  53.   List() : head(0) {} // create an empty list
  54.   List(const List<My_type> &); // copy constructor
  55.   // I suppose other member functions will be needed
  56. };
  57.  
  58. template <class My_type>
  59. List<My_type>::List(const List<My_type> &oldList)
  60. : head(0)
  61. {
  62.   if (oldList.head)
  63.   {
  64.     head = new node(oldList.head->data); // create new node with copy of
  65.                                          // old head data. That will be the
  66.                                          // new head
  67.     node *nOld = oldList.head->next;
  68.     node *nNew = head;
  69.     while (nOld)
  70.     {
  71.       nNew->next = new node(n->data) // copy the remaining nodes
  72.       nNew = nNew->next;
  73.       nOld = nOld->next
  74.     }
  75.   }
  76. }
  77.  
  78.  
  79. Note: the node class relies on a My_type copy constructor to copy the My_type 
  80. data.
  81.  
  82.  Frederic LACHASSE (ECP 86)
  83.  CompuServe: 100530,2005
  84.  Internet: lachass@worldnet.fr
  85.  
  86.